//
//  LoginViewController.m
//  Mecca
//
//  Created by Ryan Enguero on 5/20/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//  AWTS
#import "LoginViewController.h"

#import "UIManager.h"

#import "PostClass.h"

#import "ParserClass.h"

#import "IIViewDeckController.h"

#import "Reachability.h"

#import "UserProfileViewController.h"

#import "GlobalVariables.h"



#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

@interface LoginViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>{
    
    NSDictionary *jsonForFb;
    NSString *fbEmail;
    NSString *fbID;
    NSString *fbName;
    NSArray *permit;
    
}

@property (strong, nonatomic) IBOutlet FBLoginView *fbLoginView1;
@property (strong, nonatomic) NSString *objectID;



@end

@implementation LoginViewController
NSString *loginType;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    permit = [NSArray arrayWithObjects:@"public_profile",@"user_birthday",@"email", nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappens:) name:@"userProfile" object:nil];
    
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
    
    
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    if (internetStatus != NotReachable){
        //[_fbLoginView1 setReadPermissions:@[@"public_profile"]];
        [_fbLoginView1 setReadPermissions:permit];
        [_fbLoginView1 setDelegate:self];
        _objectID = nil;
    }else{
        UIAlertView *noNet = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please Check your Internet Connection" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        
        [noNet show];
        
    }
    
    self.usernameTxt.text = @"ryanenguero";
    self.passwordTxt.text = @"ryan";
    
//    self.usernameTxt.text = @"nicoaureliovillanueva@yahoo.com";
//    self.passwordTxt.text = @"s";
    
    self.activityLoader.hidden = YES;
    self.btnLogin.userInteractionEnabled = YES;
    
    self.actLoader.hidden = YES;
    
    [self.navigationController.navigationBar setHidden:YES];

    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

-(void) somethingHappens:(NSNotification*) notification
{
    NSLog(@"notification = %@",[notification object]);
    
    GlobalVariables *gBar = [GlobalVariables sharedItems];
    
    if ([gBar.loginType isEqualToString:@"normal"]) {
        self.receivedDataRefresh = [NSMutableData data]; // Data
        NSURLRequest *request = [[PostClass sharedItems] loginWithUsername:self.usernameTxt.text andPassword:self.passwordTxt.text andDeviceID:[UIDevice currentDevice].identifierForVendor.UUIDString];
        _theConnectionRefresh = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }else if ([gBar.loginType isEqualToString:@"fb"]){
    
        jsonForFb = [[PostClass sharedItems] postLoginType:@"fb" andFbID:fbID andUserDname:fbName andUserEmail:fbEmail];
        
        [[ParserClass sharedItems] userDetails:jsonForFb];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"userProfileView" object:@"userProfileView"]; 
    
    }
    
   
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loginAction:(id)sender {
    
    GlobalVariables *gvar = [GlobalVariables sharedItems];
    
    if([self.usernameTxt.text isEqualToString:@""] || [self.passwordTxt.text isEqualToString:@""] ) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login Failed!" message:@"Please enter both Username and Password" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alertView show];
        
        
        self.activityLoader.hidden = YES;
        
    }else{
    
        
         gvar.loginType = @"normal";
        
        self.activityLoader.hidden = NO;
        
    self.receivedData = [NSMutableData data]; // Data
    NSURLRequest *request = [[PostClass sharedItems] loginWithUsername:self.usernameTxt.text andPassword:self.passwordTxt.text andDeviceID:[UIDevice currentDevice].identifierForVendor.UUIDString];
    _theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        
    }

}

- (IBAction)registerAction:(id)sender {
    
    [self.navigationController pushViewController:[[UIManager sharedItems] registerViewController] animated:YES];
    
}

#pragma mark - UITextField Delegate

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (self.view.frame.size.height == 568) {
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 85);
                         }
                         completion:nil
         ];
    }else{
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 180);
                         }
                         completion:nil
         ];
    }
    return YES;
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    if (self.view.frame.size.height == 568) {
        
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
        
    }else{
        [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionTransitionNone
                         animations:^{
                             self.scroller.contentOffset = CGPointMake(0, 0);
                             
                         }
                         completion:nil
         ];
    }
    
    return YES;
}




#pragma mark Facebook

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user{
    
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    GlobalVariables *gvar = [GlobalVariables sharedItems];
    
    if (internetStatus != NotReachable) {
        //my web-dependent code
        
        
        [self.activityLoader setHidden:NO];
        [self.usernameTxt setUserInteractionEnabled:NO];
        [self.passwordTxt setUserInteractionEnabled:NO];
        
        fbID = user.objectID;
        fbName = user.name;
        
        

        
        
//        jsonForFb = [[PostClass sharedItems] postLoginType:@"fb" andFbID:user.objectID andUserDname:user.name andUserEmail:fbEmail];
//        NSLog(@"Your Have A Internet Conection");
        gvar.loginType = @"fb";

//        [[ParserClass sharedItems] userDetails:jsonForFb];
        
        
    }
    
    
    
    
}


- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
    NSLog(@"User logged in");
    
    //NSArray *permissionsNeeded = @[@"public_profile", @"user_birthday", @"email"];
    
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  // These are the current permissions the user has
                                  NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                  
                                  // We will store here the missing permissions that we will have to request
                                  NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];
                                  
                                  // Check if all the permissions we need are present in the user's current permissions
                                  // If they are not present add them to the permissions to be requested
                                  for (NSString *permission in permit){
                                      if (![currentPermissions objectForKey:permission]){
                                          [requestPermissions addObject:permission];
                                      }
                                  }
                                  
                                  // If we have permissions to request
                                  if ([requestPermissions count] > 0){
                                      // Ask for the missing permissions
                                      [FBSession.activeSession
                                       requestNewReadPermissions:requestPermissions
                                       completionHandler:^(FBSession *session, NSError *error) {
                                           if (!error) {
                                               // Permission granted, we can request the user information
                                               [self makeRequestForUserData];
                                           } else {
                                               // An error occurred, we need to handle the error
                                               // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                               NSLog(@"error %@", error.description);
                                           }
                                       }];
                                  } else {
                                      // Permissions are present
                                      // We can request the user information
                                      [self makeRequestForUserData];
                                  }
                                  
                              } else {
                                  // An error occurred, we need to handle the error
                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"error %@", error.description);
                              }
                          }];
    


    
}

// Implement the loginViewShowingLoggedOutUser: delegate method to modify your app's UI for a logged-out user experience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
    NSLog(@"User logged out");
    
    [[UIManager sharedItems] logOut];

}


// You need to override loginView:handleError in order to handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
    NSString *alertMessage, *alertTitle;
    
    // If the user should perform an action outside of you app to recover,
    // the SDK will provide a message for the user, you just need to surface it.
    // This conveniently handles cases like Facebook password change or unverified Facebook accounts.
    if ([FBErrorUtility shouldNotifyUserForError:error]) {
        alertTitle = @"Facebook error";
        alertMessage = [FBErrorUtility userMessageForError:error];
        
        // This code will handle session closures since that happen outside of the app.
        // You can take a look at our error handling guide to know more about it
        // https://developers.facebook.com/docs/ios/errors
    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
        alertTitle = @"Session Error";
        alertMessage = @"Your current session is no longer valid. Please log in again.";
        
        // If the user has cancelled a login, we will do nothing.
        // You can also choose to show the user a message if cancelling login will result in
        // the user not being able to complete a task they had initiated in your app
        // (like accessing FB-stored information or posting to Facebook)
    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
        NSLog(@"user cancelled login");
        
        // For simplicity, this sample handles other errors with a generic message
        // You can checkout our error handling guide for more detailed information
        // https://developers.facebook.com/docs/ios/errors
    } else {
        alertTitle  = @"Something went wrong";
        alertMessage = @"Please try again later.";
        NSLog(@"Unexpected error:%@", error);
    }
    
    if (alertMessage) {
        [[[UIAlertView alloc] initWithTitle:alertTitle
                                    message:alertMessage
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    }
}

// ------------> Login code ends here <------------


// ------------> Code for requesting user information starts here <------------

/*
 This function asks for the user's public profile and birthday.
 It first checks for the existence of the public_profile and user_birthday permissions
 If the permissions are not present, it requests them
 If/once the permissions are present, it makes the user info request
 */

- (IBAction)requestUserInfo:(id)sender
{
    // We will request the user's public picture and the user's birthday
    // These are the permissions we need:
    NSArray *permissionsNeeded = @[@"public_profile", @"user_birthday"];
    
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  // These are the current permissions the user has
                                  NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                  
                                  // We will store here the missing permissions that we will have to request
                                  NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];
                                  
                                  // Check if all the permissions we need are present in the user's current permissions
                                  // If they are not present add them to the permissions to be requested
                                  for (NSString *permission in permissionsNeeded){
                                      if (![currentPermissions objectForKey:permission]){
                                          [requestPermissions addObject:permission];
                                      }
                                  }
                                  
                                  // If we have permissions to request
                                  if ([requestPermissions count] > 0){
                                      // Ask for the missing permissions
                                      [FBSession.activeSession
                                       requestNewReadPermissions:requestPermissions
                                       completionHandler:^(FBSession *session, NSError *error) {
                                           if (!error) {
                                               // Permission granted, we can request the user information
                                               [self makeRequestForUserData];
                                           } else {
                                               // An error occurred, we need to handle the error
                                               // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                               NSLog(@"error %@", error.description);
                                           }
                                       }];
                                  } else {
                                      // Permissions are present
                                      // We can request the user information
                                      [self makeRequestForUserData];
                                  }
                                  
                              } else {
                                  // An error occurred, we need to handle the error
                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"error %@", error.description);
                              }
                          }];
    
    
    
}

- (void) makeRequestForUserData
{
    [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            // Success! Include your code to handle the results here
            NSLog(@"user info: %@", result);
            fbEmail = [result objectForKey:@"email"];
            NSLog(@"Variable = %@", fbEmail);
            
            
            NSLog(@"ID: %@ Name:%@",fbID, fbName);
            
            jsonForFb = [[PostClass sharedItems] postLoginType:@"fb" andFbID:fbID andUserDname:fbName andUserEmail:fbEmail];
            
            [[ParserClass sharedItems] userDetails:jsonForFb];
            
            
            NSString *status = [NSString stringWithFormat:@"%@", [jsonForFb objectForKey:@"Status"]];
            
            if ([status isEqualToString:@"Success"]) {
                NSLog(@"Success");
                
                NSLog(@"Datas Json for FB = %@",jsonForFb);
                
                
                GlobalVariables *gVar = [GlobalVariables sharedItems];
                
                gVar.loginType = @"fb";
                
                [self.activityLoader setHidden:YES];
                [self.usernameTxt setUserInteractionEnabled:YES];
                [self.passwordTxt setUserInteractionEnabled:YES];
                
                
                
//                dispatch_async(dispatch_get_main_queue(), ^{
                
                    IIViewDeckController *deckView = [[UIManager sharedItems] generateControllerStack];
                    
                    self.activityLoader.hidden = YES;
                    self.btnLogin.userInteractionEnabled = YES;
                    [self.navigationController pushViewController:deckView animated:YES];
                    
                    
                    
//                });
                
            }
            
            
        } else {
            // An error occurred, we need to handle the error
            // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
            NSLog(@"error %@", error.description);  
        }
    }];
}

// ------------> Code for requesting user information ends here <------------

// ------------> Code for requesting user events starts here <------------

/*
 This function asks for the user's (upcoming) events, and for those events retrieves the name, the start_time and the cover picture.
 It first checks for the existence of the public_profile and user_events permissions
 If the permissions are not present, it requests them
 If/once the permissions are present, it makes the user events request with field expansion for name, start_time and cover picture.
 */

- (IBAction)requestEvents:(id)sender
{
    // We will request the user's events
    // These are the permissions we need:
    NSArray *permissionsNeeded = @[@"user_events"];
    
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                  NSLog(@"current permissions %@", currentPermissions);
                                  NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];
                                  
                                  // Check if all the permissions we need are present in the user's current permissions
                                  // If they are not present add them to the permissions to be requested
                                  for (NSString *permission in permissionsNeeded){
                                      if (![currentPermissions objectForKey:permission]){
                                          [requestPermissions addObject:permission];
                                      }
                                  }
                                  
                                  // If we have permissions to request
                                  if ([requestPermissions count] > 0){
                                      // Ask for the missing permissions
                                      [FBSession.activeSession requestNewReadPermissions:requestPermissions
                                                                       completionHandler:^(FBSession *session, NSError *error) {
                                                                           if (!error) {
                                                                               // Permission granted
                                                                               NSLog(@"new permissions %@", [FBSession.activeSession permissions]);
                                                                               // We can request the user information
                                                                               [self makeRequestForUserEvents];
                                                                           } else {
                                                                               // An error occurred, we need to handle the error
                                                                               // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                                                               NSLog(@"error %@", error.description);
                                                                           }
                                                                       }];
                                  } else {
                                      // Permissions are present
                                      // We can request the user information
                                      [self makeRequestForUserEvents];
                                  }
                                  
                              } else {
                                  // An error occurred, we need to handle the error
                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"error %@", error.description);
                              }
                          }];
}

- (void)makeRequestForUserEvents
{
    [FBRequestConnection startWithGraphPath:@"me/events?fields=cover,name,start_time"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error) {
                                  // Success! Include your code to handle the results here
                                  NSLog(@"user events: %@", result);
                              } else {
                                  // An error occurred, we need to handle the error
                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"error %@", error.description);
                              }
                          }];
}

// ------------> Code for requesting user events ends here <------------

// ------------> Code for posting an object starts here <------------

/*
 This function posts an object to the Facebook graph.
 The object is of type restaurant.restaurant, one of the default object types that Facebook provides.
 The object has an image, which is stored on the Facebook staging service, code for staging the image is provided.
 First we need to ask for the publish_actions permission. If the permission is granted, we proceed to pick an
 image from the photo gallery, stage this image on the Facebook staging service, then create the object and
 set all its properties (standard, inherited, and its own type's) including the image, and finally we make
 the call to the Graph API to post the object.
 */

- (IBAction)postObject:(id)sender
{
    
    // We will post an object on behalf of the user
    // These are the permissions we need:
    NSArray *permissionsNeeded = @[@"publish_actions"];
    
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                  NSLog(@"current permissions %@", currentPermissions);
                                  NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];
                                  
                                  // Check if all the permissions we need are present in the user's current permissions
                                  // If they are not present add them to the permissions to be requested
                                  for (NSString *permission in permissionsNeeded){
                                      if (![currentPermissions objectForKey:permission]){
                                          [requestPermissions addObject:permission];
                                      }
                                  }
                                  
                                  // If we have permissions to request
                                  if ([requestPermissions count] > 0){
                                      // Ask for the missing permissions
                                      [FBSession.activeSession requestNewPublishPermissions:requestPermissions
                                                                            defaultAudience:FBSessionDefaultAudienceFriends
                                                                          completionHandler:^(FBSession *session, NSError *error) {
                                                                              if (!error) {
                                                                                  // Permission granted
                                                                                  NSLog(@"new permissions %@", [FBSession.activeSession permissions]);
                                                                                  // We can request the user information
                                                                                  [self makeRequestToPostObject];
                                                                              } else {
                                                                                  // An error occurred, we need to handle the error
                                                                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                                                                  NSLog(@"error %@", error.description);
                                                                              }
                                                                          }];
                                  } else {
                                      // Permissions are present
                                      // We can request the user information
                                      [self makeRequestToPostObject];
                                  }
                                  
                              } else {
                                  // An error occurred, we need to handle the error
                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"error %@", error.description);
                              }
                          }];
}

- (void)makeRequestToPostObject
{
    // Retrieve a picture from the device's photo library
    /*
     NOTE: SDK Image size limits are 480x480px minimum resolution to 12MB maximum file size.
     In this app we're not making sure that our image is within those limits but you should.
     Error code for images that go below or above the size limits is 102.
     */
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [imagePicker setDelegate:self];
    [self presentViewController:imagePicker animated:YES completion:nil];
}

// When the user is done picking the image
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // Get the UIImage
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    // Dismiss the image picker off the screen
    [self dismissViewControllerAnimated:YES completion:nil];
    
    // stage the image
    [FBRequestConnection startForUploadStagingResourceWithImage:image completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        __block NSString *alertText;
        __block NSString *alertTitle;
        if(!error) {
            NSLog(@"Successfuly staged image with staged URI: %@", [result objectForKey:@"uri"]);
            
            // Package image inside a dictionary, inside an array like we'll need it for the object
            NSArray *image = @[@{@"url": [result objectForKey:@"uri"], @"user_generated" : @"true" }];
            
            // Create an object
            NSMutableDictionary<FBOpenGraphObject> *restaurant = [FBGraphObject openGraphObjectForPost];
            
            // specify that this Open Graph object will be posted to Facebook
            restaurant.provisionedForPost = YES;
            
            // Add the standard object properties
            restaurant[@"og"] = @{ @"title":@"mytitle", @"type":@"restaurant.restaurant", @"description":@"my description", @"image":image };
            
            // Add the properties restaurant inherits from place
            restaurant[@"place"] = @{ @"location" : @{ @"longitude": @"-58.381667", @"latitude":@"-34.603333"} };
            
            // Add the properties particular to the type restaurant.restaurant
            restaurant[@"restaurant"] = @{@"category": @[@"Mexican"],
                                          @"contact_info": @{@"street_address": @"123 Some st",
                                                             @"locality": @"Menlo Park",
                                                             @"region": @"CA",
                                                             @"phone_number": @"555-555-555",
                                                             @"website": @"http://www.example.com"}};
            
            // Make the Graph API request to post the object
            FBRequest *request = [FBRequest requestForPostWithGraphPath:@"me/objects/restaurant.restaurant"
                                                            graphObject:@{@"object":restaurant}];
            [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if (!error) {
                    // Success! Include your code to handle the results here
                    NSLog(@"result: %@", result);
                    _objectID = [result objectForKey:@"id"];
                    alertTitle = @"Object successfully created";
                    alertText = [NSString stringWithFormat:@"An object with id %@ has been created", _objectID];
                    [[[UIAlertView alloc] initWithTitle:alertTitle
                                                message:alertText
                                               delegate:self
                                      cancelButtonTitle:@"OK!"
                                      otherButtonTitles:nil] show];
                } else {
                    // An error occurred, we need to handle the error
                    // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                    NSLog(@"error %@", error.description);
                }
            }];
        } else {
            // An error occurred, we need to handle the error
            // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
            NSLog(@"error %@", error.description);
        }
    }];
}

// ------------> Code for posting an object ends here <------------

// ------------> Code for deleting an object starts here <------------

- (IBAction)deleteObject:(id)sender
{
    __block NSString *objectID = _objectID;
    if(!objectID){
        [[[UIAlertView alloc] initWithTitle:@"Error"
                                    message:@"Can't delete an object because there's no object! Please tap the \"Post an object\" button first to create an object, then you can click on this button to delete it."
                                   delegate:self
                          cancelButtonTitle:@"OK!"
                          otherButtonTitles:nil] show];
    } else {
        //Make an HTTP DELETE request with the OG object's ID
        [FBRequestConnection startForDeleteObject:objectID
                                completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                    NSString *alertText;
                                    NSString *alertTitle;
                                    if (error) {
                                        // An error occurred, we need to handle the error
                                        // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                        NSLog(@"error %@", error.description);
                                    } else {
                                        // The object has been removed
                                        NSLog(@"The object with id %@ has been deleted", objectID);
                                        alertTitle = @"Object successfully deleted";
                                        alertText = [NSString stringWithFormat:@"The object with id %@ has been deleted", objectID];
                                        [[[UIAlertView alloc] initWithTitle:alertTitle
                                                                    message:alertText
                                                                   delegate:self
                                                          cancelButtonTitle:@"OK!"
                                                          otherButtonTitles:nil] show];
                                        objectID =  nil;
                                    }
                                }];
        _objectID = objectID;
        
    }
}

// ------------> Code for deleting an object ends here <------------

// ------------> Code for posting an OG story starts here <------------

- (IBAction)postOGStory:(id)sender
{
    // We will post a story on behalf of the user
    // These are the permissions we need:
    NSArray *permissionsNeeded = @[@"publish_actions"];
    
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                  NSLog(@"current permissions %@", currentPermissions);
                                  NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];
                                  
                                  // Check if all the permissions we need are present in the user's current permissions
                                  // If they are not present add them to the permissions to be requested
                                  for (NSString *permission in permissionsNeeded){
                                      if (![currentPermissions objectForKey:permission]){
                                          [requestPermissions addObject:permission];
                                      }
                                  }
                                  
                                  // If we have permissions to request
                                  if ([requestPermissions count] > 0){
                                      // Ask for the missing permissions
                                      [FBSession.activeSession requestNewPublishPermissions:requestPermissions
                                                                            defaultAudience:FBSessionDefaultAudienceFriends
                                                                          completionHandler:^(FBSession *session, NSError *error) {
                                                                              if (!error) {
                                                                                  // Permission granted
                                                                                  NSLog(@"new permissions %@", [FBSession.activeSession permissions]);
                                                                                  // We can request the user information
                                                                                  [self makeRequestToPostStory];
                                                                              } else {
                                                                                  // An error occurred, we need to handle the error
                                                                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                                                                  NSLog(@"error %@", error.description);
                                                                              }
                                                                          }];
                                  } else {
                                      // Permissions are present
                                      // We can request the user information
                                      [self makeRequestToPostStory];
                                  }
                                  
                              } else {
                                  // An error occurred, we need to handle the error
                                  // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"error %@", error.description);
                              }
                          }];
}

- (void)makeRequestToPostStory
{
    if(!_objectID){
        [[[UIAlertView alloc] initWithTitle:@"Error"
                                    message:@"Please tap the \"Post an object\" button first to create an object, then you can click on this button to like it."
                                   delegate:self
                          cancelButtonTitle:@"OK!"
                          otherButtonTitles:nil] show];
    } else {
        // Create a like action
        id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
        
        // Link that like action to the restaurant object that we have created
        [action setObject:_objectID forKey:@"object"];
        
        // Post the action to Facebook
        [FBRequestConnection startForPostWithGraphPath:@"me/og.likes"
                                           graphObject:action
                                     completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                         __block NSString *alertText;
                                         __block NSString *alertTitle;
                                         if (!error) {
                                             // Success, the restaurant has been liked
                                             NSLog(@"Posted OG action, id: %@", [result objectForKey:@"id"]);
                                             alertText = [NSString stringWithFormat:@"Posted OG action, id: %@", [result objectForKey:@"id"]];
                                             alertTitle = @"Success";
                                             [[[UIAlertView alloc] initWithTitle:alertTitle
                                                                         message:alertText
                                                                        delegate:self
                                                               cancelButtonTitle:@"OK!"
                                                               otherButtonTitles:nil] show];
                                         } else {
                                             // An error occurred, we need to handle the error
                                             // Check out our error handling guide: https://developers.facebook.com/docs/ios/errors/
                                             NSLog(@"error %@", error.description);
                                         }
                                     }];
        
    }
}

// ------------> Code for posting an OG story ends here <------------



//// You need to override loginView:handleError in order to handle possible errors that can occur during login
//- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
//    NSString *alertMessage, *alertTitle;
//    
//    // If the user should perform an action outside of you app to recover,
//    // the SDK will provide a message for the user, you just need to surface it.
//    // This conveniently handles cases like Facebook password change or unverified Facebook accounts.
//    if ([FBErrorUtility shouldNotifyUserForError:error]) {
//        alertTitle = @"Facebook error";
//        alertMessage = [FBErrorUtility userMessageForError:error];
//        
//        // This code will handle session closures since that happen outside of the app.
//        // You can take a look at our error handling guide to know more about it
//        // https://developers.facebook.com/docs/ios/errors
//    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
//        alertTitle = @"Session Error";
//        alertMessage = @"Your current session is no longer valid. Please log in again.";
//        
//        // If the user has cancelled a login, we will do nothing.
//        // You can also choose to show the user a message if cancelling login will result in
//        // the user not being able to complete a task they had initiated in your app
//        // (like accessing FB-stored information or posting to Facebook)
//    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
//        NSLog(@"user cancelled login");
//        
//        // For simplicity, this sample handles other errors with a generic message
//        // You can checkout our error handling guide for more detailed information
//        // https://developers.facebook.com/docs/ios/errors
//    } else {
//        alertTitle  = @"Something went wrong";
//        alertMessage = @"Please try again later.";
//        NSLog(@"Unexpected error:%@", error);
//    }
//    
//    if (alertMessage) {
//        [[[UIAlertView alloc] initWithTitle:alertTitle
//                                    message:alertMessage
//                                   delegate:nil
//                          cancelButtonTitle:@"OK"
//                          otherButtonTitles:nil] show];
//    }
//}

#pragma mark - URL Connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    
    if (connection == self.theConnection) {
         [_receivedData setLength:0];
    }else if (connection == self.theConnectionRefresh){
        [_receivedDataRefresh setLength:0];
    }
   
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error data = %@", error);
    
    self.activityLoader.hidden = YES;
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Failed" message:@"Please check you internet connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    [alertView show];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    
    
    if (connection == self.theConnection) {
        
        [_receivedData appendData:data];
        
          NSLog(@"data parse = %@", _receivedData);
        
    }else if (connection == self.theConnectionRefresh){
    
        [_receivedDataRefresh appendData:data];
        
        NSLog(@"data parse = %@", _receivedDataRefresh);
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    if (connection == self.theConnection) {
        
        
        
        if (!(_receivedData == nil)) {
            NSError *error;
            
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:_receivedData options:kNilOptions error:&error];
            
            NSLog(@"Received Data = %@", json);
            
            
            NSString *status = [NSString stringWithFormat:@"%@", [json objectForKey:@"Status"]];
            
            if ([status isEqualToString:@"Success"]) {
                NSLog(@"Success");
                
                [[ParserClass sharedItems] userDetails:json];
                
                
                self.actLoader.hidden = YES;
    
                    IIViewDeckController *deckView = [[UIManager sharedItems] generateControllerStack];
                
                    self.activityLoader.hidden = YES;
                    self.btnLogin.userInteractionEnabled = YES;
                    [self.navigationController pushViewController:deckView animated:YES];
                
            }else if ([status isEqualToString:@"0"]){
                
                NSLog(@"Error");
                
                    self.btnLogin.userInteractionEnabled = YES;

                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Username or Password are invalid" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    
                    self.activityLoader.hidden = YES;
                
                    [alertView show];
            }
        }
        
        
        NSLog(@"Finish");
    }else if (connection == self.theConnectionRefresh) {
        
        
        if (!(_receivedDataRefresh == nil)) {
            NSError *error;
            
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:_receivedDataRefresh options:kNilOptions error:&error];
            
            NSLog(@"Received Data = %@", json);
            
            [[ParserClass sharedItems] userDetails:json];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"userProfileView" object:@"userProfileView"];

            
        }
        
        
        NSLog(@"Finish");
    }
    
}

@end
